﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Diploma.Models;
using Diploma.Validation;
using System.IO;

namespace DiplomaWeb.Controllers
{
    [Authorize]
    public class NormControlDocsController : MasterController
    {
        public NormControlDocsController(DataManager dataManager)
            : base(dataManager)
        {
        }

        public ActionResult GetDoc(Guid id)
        {
            NormControlDoc doc = DataManager.NormControlDocsRepository.GetDoc(id);
            if (doc == null)
                throw new NullReferenceException("Normal control document with specified id not founded");

            return File(doc.DocFile, "application/doc", Path.GetFileName(doc.FileName) + ".doc");
        }

        public ActionResult GetPdf(Guid id)
        {
            NormControlDoc doc = DataManager.NormControlDocsRepository.GetDoc(id);
            if (doc == null)
                throw new NullReferenceException("Normal control document with specified id not founded");

            return File(doc.PdfFile, "application/pdf", Path.GetFileName(doc.FileName) + ".pdf");
        }

        [HttpPost]
        public ActionResult Add(Guid StudentId, HttpPostedFileBase postFile)
        {
            Student student = DataManager.StudentsRepository.GetStudent(StudentId);
            if (student == null)
                throw new NullReferenceException("Student with specified id not found");
            DataManager.NormControlDocsRepository.Add(postFile, student);

            return new EmptyResult();
        }

        public ActionResult SetCompletion(Guid id, bool isCompleted) 
        {
            NormControlDoc doc = DataManager.NormControlDocsRepository.GetDoc(id);
            if (doc == null)
                throw new NullReferenceException("Normal control document with specified id not founded");
            DataManager.NormControlDocsRepository.SetCompletion(doc, isCompleted);

            ViewData.Model = doc;

            return PartialView("~/Views/NormControlDocs/NormControlDocViewControl.ascx");
        }
    }
}